package com.bartender.consumer.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.bartender.common.annotation.Log;
import com.bartender.common.core.controller.BaseController;
import com.bartender.common.core.domain.AjaxResult;
import com.bartender.common.enums.BusinessType;
import com.bartender.consumer.domain.WxUser;
import com.bartender.consumer.service.IWxUserService;
import com.bartender.common.utils.poi.ExcelUtil;
import com.bartender.common.core.page.TableDataInfo;

/**
 * 微信用户管理Controller
 * 
 * @author bartender
 * @date 2024-12-05
 */
@RestController
@RequestMapping("/consumer/wx_user")
public class WxUserController extends BaseController
{
    @Autowired
    private IWxUserService wxUserService;

    /**
     * 查询微信用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('consumer:wx_user:list')")
    @GetMapping("/list")
    public TableDataInfo list(WxUser wxUser)
    {
        startPage();
        List<WxUser> list = wxUserService.selectWxUserList(wxUser);
        return getDataTable(list);
    }

    /**
     * 导出微信用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('consumer:wx_user:export')")
    @Log(title = "微信用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxUser wxUser)
    {
        List<WxUser> list = wxUserService.selectWxUserList(wxUser);
        ExcelUtil<WxUser> util = new ExcelUtil<WxUser>(WxUser.class);
        util.exportExcel(response, list, "微信用户管理数据");
    }

    /**
     * 获取微信用户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('consumer:wx_user:query')")
    @GetMapping(value = "/{wxUserId}")
    public AjaxResult getInfo(@PathVariable("wxUserId") Long wxUserId)
    {
        return success(wxUserService.selectWxUserByWxUserId(wxUserId));
    }

//    @GetMapping("/list_front")
//    public WxUser listFront(@RequestAttribute("openid") String openid)
//    {
//        if (wxUserService.selectWxUserByOpenid(openid) == null){
//            return null;
//        }
//        WxUser wxUser = wxUserService.selectWxUserByOpenid(openid);
//        return wxUser;
//    }

    /**
     * 新增微信用户管理
     */
    @PreAuthorize("@ss.hasPermi('consumer:wx_user:add')")
    @Log(title = "微信用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxUser wxUser)
    {
        return toAjax(wxUserService.insertWxUser(wxUser));
    }

    /**
     * 修改微信用户管理
     */
    @PreAuthorize("@ss.hasPermi('consumer:wx_user:edit')")
    @Log(title = "微信用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxUser wxUser)
    {
        return toAjax(wxUserService.updateWxUser(wxUser));
    }

    @PutMapping("updateBySelf")
    public AjaxResult updateBySelf(@RequestAttribute("openid") String openid, @RequestBody WxUser wxUser)
    {
        if (wxUserService.selectWxUserByOpenid(openid) == null){
            return error("用户不存在");
        }
        wxUser.setOpenid(openid);
        return toAjax(wxUserService.updateWxUserByOpenid(wxUser));
    }

    /**
     * 删除微信用户管理
     */
    @PreAuthorize("@ss.hasPermi('consumer:wx_user:remove')")
    @Log(title = "微信用户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{wxUserIds}")
    public AjaxResult remove(@PathVariable Long[] wxUserIds)
    {
        return toAjax(wxUserService.deleteWxUserByWxUserIds(wxUserIds));
    }
}
